package com.meilele.controller.zhengchao;


import com.meilele.biz.zhengchao.ArticleBiz;
import com.meilele.biz.zhengchao.ImageBiz;
import com.meilele.biz.zhengchao.LableBiz;
import com.meilele.entity.zhengchao.*;
import com.meilele.util.zhengchao.Contants;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private ArticleBiz biz;
    @Resource
    private LableBiz bizz;
    @Resource
    private ImageBiz imageBiz;
    /**
     * 跳转子目录的页面:根据文章的类型查找对应的文章
     * @bigtype:文章类型的id
     * @pageNum:当前是第几页
     */
    @RequestMapping("/zhuangxiu/{bigtype}")
    public String findByType(@PathVariable("bigtype") Integer bigType, Integer pageNum, Map<String,Object> request){
        List<FourArticle> article=new ArrayList();
        List<FourLable> lables=new ArrayList();
        lables=bizz.findLableByTypeId(bigType);
        article= biz.findArticleByType(bigType,pageNum);
        long totalCount=biz.getCount(bigType);
        request.put("article",article);
        request.put("lable",lables);
        request.put("totalCount",totalCount);
        request.put("pageNum", pageNum);
        return "four/indexinfo";
    }

    /**
     * 跳转装修攻略首页
     * @param model
     * @return :首页
     */
    @RequestMapping("/index/{id}")
    public String index(@PathVariable("id")int id, Model model){
        List<HotArticle> hotArticle=biz.orderByReader();
        FourArticle numberOne=hotArticle.get(0).getFourArticle();
        hotArticle.remove(0);
        List<FourArticle> articleTime=biz.orderByTime(id);
        List<FourIndexImage> image=imageBiz.findAll();
        model.addAttribute("one",numberOne);
        model.addAttribute("hot",hotArticle);
        model.addAttribute("tamer",articleTime);
        model.addAttribute("img",image);
        model.addAttribute("id",id);
        return "four/index";
    }

    @ResponseBody
    @RequestMapping("/jiazai")
    public List<FourArticle> findOther(int id, HttpSession session){
        int ss=id+1;
        session.setAttribute("id",ss);
        List<FourArticle> aa =biz.orderByTime(ss);
        return aa;
    }
    /**
     * @return : 后台管理装修攻略管理页面
     */
    @RequestMapping("/mangerIndex")
     @RequiresPermissions("admin:admin")
    public String articleIndex(Model model){
        /*return "four/index";*/
        List<FourIndexImage> list=imageBiz.findAll();
        model.addAttribute("list",list);
        return "four/manager/index";

    }

    /**
     *
     * @param model
     * @param pageNum:当前的页码
     * @return :跳转后台文章详情管理
     */
    @RequestMapping("/findAll")
    public String findAll(Model model,int pageNum){
       List<FourArticle> list= biz.findAll(pageNum);
        long totalCount=biz.getCount1();
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("list",list);
        model.addAttribute("number", Contants.PAGE_NUMBER);
       return "four/manager/articleIndex";
    }

    /**
     *
     * @return :后台管理添加页面
     */
    @RequestMapping("/preSave")
    public String preSave(){
        return "four/manager/articleSave";
    }

    /**
     *
     * @param article
     * @param request
     * @return : 提交文章添加
     */
    @ResponseBody
    @RequestMapping("/save")
    public AjaxMsgModel save (FourArticle article,MultipartHttpServletRequest request){
        MultipartFile photo = request.getFile("photo");
        String fileName = UUID.randomUUID().toString();
        String path=Contants.IMAGE_PATH + fileName;
        File newFile = new File(path);
        try {
            photo.transferTo(newFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        article.setArticleImage(fileName);
        biz.save(article);
        return  new AjaxMsgModel("200","添加成功","artilce","","closeCurrent","");

    }

    /**
     *
     * @param id
     * @param request
     * @return :根据id修改文章
     */
    @RequestMapping("/preUpdate/{id}")
    public String preUpdate(@PathVariable("id") int id, Model request){
       FourArticle article= biz.findById(id);
       request.addAttribute("article",article);
        return "four/manager/update";
    }

    /**
     * 提交修改的内容
     * @param article
     * @param id:文章的id
     * @param request
     * @return :跳转首页
     */
    @ResponseBody
    @RequestMapping("update/{id}")
    public AjaxMsgModel updateById(FourArticle article,@PathVariable("id") int id,MultipartHttpServletRequest request){
        if(!(request.getFile("photo").isEmpty())){
            MultipartFile photo = request.getFile("photo");
            String fileName = UUID.randomUUID().toString();
            String path = Contants.IMAGE_PATH + fileName;
            File newFile = new File(path);
            try {
                photo.transferTo(newFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
            article.setArticleImage(fileName);
        }
            article.setArticleId(id);
        System.out.println(article.getFourContent().toString());
        biz.updateById(article);
        return new AjaxMsgModel("200","修改成功","artilce","","closeCurrent","");
    }

    /**
     * 根据id删除文章
     * @param id
     * @return :跳转主页面
     */
    @ResponseBody
    @RequestMapping("/deleteById/{id}")
    public AjaxMsgModel deleteById(@PathVariable("id") int id){
        biz.deleteById(id);
        return new AjaxMsgModel("200","删除成功","artilce","","closeCurrent","");
    }

    /**
     * 后台展示文章的详情
     * @param id
     * @param model
     * @return :跳转详情页面
     */
    @RequestMapping("/showById/{id}")
    public String findContentById(@PathVariable("id")int id, Model model){
            FourContent content=biz.findContentById(id);
            model.addAttribute("content",content);
        return "four/manager/article_show";
    }
}
